package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.RawMaterial;
import com.ruoyi.system.mapper.RawMaterialMapper;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 定时任务定时清理过期原材料
 *
 * @author liukangzu
 */
@Component("RawTask")
public class RawTask {

      //注入Mapper
    private RawMaterialMapper rawMaterialMapper = SpringUtils.getBean(RawMaterialMapper.class);

//    @Autowired
//    private RawMaterialMapper rawMaterialMapper;

    public void cleanExpiredRawMaterials(){
        //获取原材料列表
        List<RawMaterial> rawMaterials = rawMaterialMapper.selectRawMaterialList(null);

        //获取当前时间
        LocalDate now = LocalDate.now();

        for (RawMaterial rawMaterial : rawMaterials) {

            //获取入库时间
            LocalDate parse = LocalDate.parse((CharSequence) rawMaterial.getRawMaterialTime());

            //获取时间差
            Long daysBetween = ChronoUnit.DAYS.between(parse, now);

            if (daysBetween >= 3 ){
                rawMaterialMapper.cleanRawMaterialByRawMaterialId(rawMaterial.getRawMaterialId());
            }

        }
    }
}
